package com.example.qianfeng.image.stream;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.io.File;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

public class DeleteQuartz  extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        delete();
    }

    /**
     * 执行删除文件的方法
     */
    public void delete(){
        System.out.println(new Date()+"触发定时任务执行");
        String basePath= "D:/pictures";
        String imgPath=basePath+"/statics/img";
        File file=new File(imgPath);
        File[] files = file.listFiles();
        String baseName = "";
        for (File file1 : files) {
            String fileName=file1.getName();
            if(!fileName.contains("timeOut")){
                //String baseName = FilenameUtils.getBaseName(fileName);
                baseName = "user_img_2023-10-21 11:00:23";
                String[] s = baseName.split("_");
                long timestamp = getTimeStampSys(s[2]);
                Date date=new Date(Long.parseLong(String.valueOf(timestamp)));  //获取图片上传日期
                //当前日期
                Calendar calendar=Calendar.getInstance();
                Date nowTime = calendar.getTime();

                //图片上传日期第30天的日期
                calendar.setTime(date);
                // calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+30);
                // 图片上传保存一个月
                calendar = initCalendar(calendar);
                Date outTime = calendar.getTime();
                if(nowTime.after(outTime)){ //当前日期在过期日期之后
                    System.out.println("删除过期照片:"+ fileName);
                    file1.delete();
                }
            }
        }
        System.out.println(new Date()+"定时任务执行完毕");
    }

    /**
     * 初始化 Calendar 判断几月份
     *
     * @param calendar calendar
     * @return Calendar
     */
    private Calendar initCalendar(Calendar calendar) {
        int month = LocalDate.now().getMonthValue(); // 获取月份
        if (month == 2) {
            int year = LocalDate.now().getYear(); // 获取年份
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { // 闰年
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+29);
            }else { // 平年
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+28);
            }
            return calendar;
        }
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+31);
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+30);
                break;
            default:
                break;
        }
        return calendar;
    }


    /**
     * 把上传图片的时间字符串转换成时间戳
     *
     * @param dateString 时间字符串
     * @return long
     */
    private long getTimeStampSys(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long timestamp = 0;
        try {
            Date date = sdf.parse(dateString);
            timestamp = date.getTime();
            return timestamp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
